home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14276 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: ix.netcom.com!news
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Special sort algorithm
  5. Date: 29 Mar 1996 16:05:48 GMT
  6. Organization: Netcom
  7. Message-ID: <4jh1os$scq@dfw-ixnews6.ix.netcom.com>
  8. References: <31593E09.29A0@kmd.dk>
  9. NNTP-Posting-Host: den-co11-03.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Fri Mar 29 10:05:48 AM CST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. >It must :
  16. > - Sort big files of short lines
  17. >   lines are uneven length
  18. >   files are XX MB
  19. >   lines are < 100 chars
  20. > - Put resulting file in original file
  21. > - Run in very little memory ( < 50 K)
  22. > - Use about no extra disk space ( < 10% extra)
  23. > - Run reasonably fast
  24. >
  25. >I have tried implementing in-file bubblesort and others with the language
  26. >being C++. But they are way too slow, even with some optimizing buffers.
  27.  
  28. Have you tried in-place QuickSort?  It would only be NlogN instead
  29. of N*N.  But assuming you've tried that...  This will work with about
  30. 20% extra disk space, and should be faster than the in-place quicksort.
  31.  
  32. Since you've got the disk space limitation, I would try the following:
  33.  
  34. 1) Create an index file for the file you want to sort (e.g., a file
  35.    containing binary 4-byte integers, one per line, set to the
  36.    offset within the source file of the beginning of each line.
  37.  
  38. 2) Sort the index file (i.e. treat the indices as pointers into the
  39.    source file and sort the indices such that the corresponding line
  40.    are sorted), probably using a disk-based mergesort.  For
  41.    files with an everage line length > 40 characters, the index file
  42.    will be 10% of the source file size.  The intermediate files for
  43.    the mergesort will take an additional 10%.  It will still be 
  44.    slow because you will be performing NlogN random reads in the 
  45.    source on the second and subsequent passes of the mergesort,
  46.    but that is hopefully better than NlogN reads AND writes.
  47.    The mergesort itself requires NlogN *sequential* reads/writes
  48.    of the index files, which is far better than random accesses.
  49.  
  50. 3) Once the index file is sorted, create the sorted source file
  51.    based on the index file.  This will involve N random 
  52.    reads/writes in the source file, and another 10N sequential
  53.    reads/writes (which is hopefully better than the NlogN random
  54.    reads/writes needed by an in-place quicksort).
  55.    You will also need to use another
  56.    disk buffer (but you already had to use another disk buffer
  57.    for the mergesort intermediate files, so you might as well
  58.    reuse that space).  Walk through the sorted index file, and
  59.    for each line indexed by those integers, find that line in
  60.    the source file, and write it over the beginning of the source
  61.    file.  As you do so, to avoid destroying lines that would be
  62.    overwritten, write them to the disk buffer and keep track of
  63.    M, where M is the number of lines that have been moved to
  64.    the disk buffer.  When the disk buffer reaches its 10% limit,
  65.    compact the source file by removing all lines that have been
  66.    moved to the beginning, adjust the indices, and then append
  67.    the lines from the disk buffer that have not been written to
  68.    the beginning of the source file, and adjust *those* indices
  69.    as well.  Keep going until all indices have been processed.
  70.  
  71. Disclaimer:  This is ad-hoc.  There is almost certainly a better 
  72. way to do this described somewhere in the literature.
  73.  
  74. john lilley
  75.  
  76.